//
//  JMTools.m
//  JMDome_1
//
//  Created by ios on 15/6/11.
//  Copyright (c) 2015年 ios. All rights reserved.
//

#import "JMTools.h"
#import "sys/utsname.h"
#import "Reachability.h"
#import "JMURLKey.h"
#import <AFNetworking.h>



#define iPhone4     @"iPhone4"
#define iPhone4s    @"iPhone4S"
#define iPhone5     @"iPhone5"
#define iPhone5s    @"iPhone5s"
#define iPhone6    @"iPhone6"
#define iPhone6p    @"iPhone6Plus"


@implementation JMTools

+ (void)postImageRequestAFNetWorkingDict:(NSDictionary *)dict imageArray:(NSArray *)imageArray type:(NSString *)type block:(void(^)(id data,NSError *error))block{
    
    NSString *pathurl = @"";
    if([type isEqualToString:@"1"]){
        pathurl=[HOST_URL stringByAppendingString:[NSString stringWithFormat:@"%@",POST_IMAGE_api]];
    }else if([type isEqualToString:@"2"]){
        pathurl=[HOST_URL stringByAppendingString:[NSString stringWithFormat:@"%@",Circle_UpQuestion_api]];
    }
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    [manager POST:pathurl parameters:dict constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        
    UIImage *eachImg1 = imageArray[0];
    UIImage * eachImg = [JMTools imageWithImage:eachImg1 scaledToSize:CGSizeMake(320, 150)];
    NSData *eachImgData = UIImageJPEGRepresentation(eachImg, 0.5);
        
    [formData appendPartWithFileData:eachImgData name:@"attach" fileName:@"userPhoto.jpg" mimeType:@"image/jpeg"];
   } success:^(AFHTTPRequestOperation *operation, id responseObject) {
       block(responseObject,nil);
   } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
       NSLog(@"post big file fail error=%@", error);
       block(nil,error);
   }];
}
//对图片尺寸进行压缩--
+(UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize{
    // Create a graphics image context
    UIGraphicsBeginImageContext(newSize);
    
    // Tell the old image to draw in this new context, with the desired
    // new size
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    
    // Get the new image from the context
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    // End the context
    UIGraphicsEndImageContext();
    
    // Return the new image.
    return newImage;
}

+ (BOOL)isiPhoneNumber:(NSString *)iphone{
    NSString * MOBILE = @"^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\\d{8}$";
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    BOOL res = [regextestmobile evaluateWithObject:iphone];
    return res;
}

+ (BOOL) isIDCardNumber:(NSString *)cardNumber{
    
    NSString * DIGITS = @"^(\\d{14}|\\d{17})(\\d|[xX])$";
    //    NSString * LETTEREND= @"^([0-9]){7,18}(x|X)?$";
    //    NSString * MOBILE = @"^\\d{8,18}|[0-9x]{8,18}|[0-9X]{8,18}?$";
    
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", DIGITS];
    //    NSPredicate *regextestmobile2 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", LETTEREND];
    //    NSPredicate *regextestmobile3 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    BOOL res = [regextestmobile evaluateWithObject:cardNumber];
    if(res){
        //         if([regextestmobile2 evaluateWithObject:cardNumber] || [regextestmobile3 evaluateWithObject:cardNumber]){
        return YES;
        //        }else{
        //            return NO;
        //        }
    }else{
        return NO;
    }
}
+ (NSString*)deviceString{
    // 需要#import "sys/utsname.h"
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *deviceString = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
    if ([deviceString isEqualToString:@"iPhone1,1"])    return @"iPhone1G";
    if ([deviceString isEqualToString:@"iPhone1,2"])    return @"iPhone3G";
    if ([deviceString isEqualToString:@"iPhone2,1"])    return @"iPhone3GS";
    if ([deviceString isEqualToString:@"iPhone3,1"])    return @"iPhone4";
    if ([deviceString isEqualToString:@"iPhone4,1"])    return @"iPhone4S";
    if ([deviceString isEqualToString:@"iPhone5,2"])    return @"iPhone5";
    
    if ([deviceString isEqualToString:@"iPhone7,1"])    return @"iPhone6Plus";
    if ([deviceString isEqualToString:@"iPhone7,2"])    return @"iPhone6";
    if ([deviceString isEqualToString:@"iPhone6,2"])    return @"iPhone5s";
    
    if ([deviceString isEqualToString:@"iPhone3,2"])    return @"Verizon_iPhone_4";
    if ([deviceString isEqualToString:@"iPod1,1"])      return @"iPod_Touch_1G";
    if ([deviceString isEqualToString:@"iPod2,1"])      return @"iPod_Touch_2G";
    if ([deviceString isEqualToString:@"iPod3,1"])      return @"iPod_Touch_3G";
    if ([deviceString isEqualToString:@"iPod4,1"])      return @"iPod_Touch_4G";
    if ([deviceString isEqualToString:@"iPad1,1"])      return @"iPad";
    if ([deviceString isEqualToString:@"iPad2,1"])      return @"iPad_2(WiFi)";
    if ([deviceString isEqualToString:@"iPad2,2"])      return @"iPad_2(GSM)";
    if ([deviceString isEqualToString:@"iPad2,3"])      return @"iPad_2(CDMA)";
    if ([deviceString isEqualToString:@"i386"])         return @"Simulator";
    if ([deviceString isEqualToString:@"x86_64"])       return @"Simulator";
    NSLog(@"NOTE: Unknown device type: %@", deviceString);
    return deviceString;
}
+(BOOL)isNetworkRunning;
{
    Reachability *r = [Reachability reachabilityWithHostname:@"www.baidu.com"];
    switch ([r currentReachabilityStatus]) {
        case NotReachable:
            return FALSE;
            break;
        case ReachableViaWWAN:
            return TRUE;
            break;
        case ReachableViaWiFi:
            return TRUE;
            break;
    }
    return FALSE;
    
}
+(void)showAlertView:(UIView *)aView message:(NSString *)msg{
    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:aView animated:YES];
    hud.labelText = msg;
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        sleep(1.5);
        dispatch_async(dispatch_get_main_queue(), ^{
            [self hiddenAlerView:aView];
        });
    });
}
+(void)showAlertView:(UIView *)aView{
    [MBProgressHUD showHUDAddedTo:aView animated:YES];
}

+(void)hiddenAlerView:(UIView*)aView{
    [MBProgressHUD hideAllHUDsForView:aView animated:YES];
    
}

+ (BOOL)isRightpwd:(NSString *)pw
{
    NSString *pwRegex = @"^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$";
    NSPredicate *pwTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pwRegex];
    return [pwTest evaluateWithObject:pw];
}
+ (BOOL)isValidateEmail:(NSString *)Email{
    NSString *emailCheck = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES%@",emailCheck];
    return [emailTest evaluateWithObject:Email];
}


+ (BOOL) isRightWebsite:(NSString *)url{
    NSError *error;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"http+:[^\\s]*" options:0 error:&error];
    if (regex != nil) {
        NSTextCheckingResult *firstMatch=[regex firstMatchInString:url options:0 range:NSMakeRange(0, [url length])];
        
        if (firstMatch) {
//            NSRange resultRange = [firstMatch rangeAtIndex:0]; //等同于 firstMatch.range --- 相匹配的范围
//            //从urlString当中截取数据
//            NSString *result=[url substringWithRange:resultRange];
            return YES;
            
        }else{
            return NO;
        }
    }else{
        @throw [NSException exceptionWithName:NSExpansionAttributeName reason:@"判断正则出错" userInfo:nil];
        return NO;
    }
}

@end
